﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Model.Vo;
using Sunny.Service;
using Sunny.Util.Attributes;
using Sunny.Util.Enums;

namespace Sunny.Api.Controllers
{
    [ApiController]
    [Route("/system/menu")]
    public class SysMenuController
    {
        private readonly MenuService _menuService;

        public SysMenuController(MenuService menuService)
        {
            _menuService = menuService;
        }

        #region 获取树形菜单
        [HttpGet("treeselect")]
        public async Task<Result> TreeSelect()
        {
            List<MenuTreeVo> menuTrees = await _menuService.TreeSelect();
            return Result.Success(menuTrees);
        }

        #endregion

        #region 获取角色下的树

        [HttpGet("roleMenuTreeselect/{roleId}")]
        public async Task<Result> RoleMenuTreeSelect([FromRoute]long roleId)
        {
            RoleMenuTreeSelectVo roleMenuTreeSelectVo = await _menuService.RoleMenuTreeSelect(roleId);
            return Result.Success(roleMenuTreeSelectVo);
        }
        #endregion

        #region 获取菜单列表
        [HasPermission("system:menu:list")]
        [HttpGet("list")]
        public async Task<Result> List([FromQuery]MenuListParam param)
        {
            List<SysMenu> menuList = await _menuService.MenuList(param);
            return Result.Success(menuList);
        }

        #endregion

        #region 新增菜单
        [HasPermission("system:menu:add")]
        [LogInfo(Title = "菜单管理",BusinessType = BusinessType.Insert)]
        [HttpPost]
        public async Task<Result> Add([FromBody]SysMenu sysMenu)
        {
            await _menuService.Add(sysMenu);
            return Result.Success();
        }

        #endregion

        #region 获取菜单详情
        [HasPermission("system:menu:query")]
        [HttpGet("{menuId}")]
        public async Task<Result> Info([FromRoute]long menuId)
        {
            SysMenu sysMenu = await _menuService.GetById(menuId);
            return Result.Success(sysMenu);
        }
        #endregion
        
        #region 修改菜单
        [HasPermission("system:menu:edit")]
        [LogInfo(Title = "菜单管理",BusinessType = BusinessType.Update)]
        [HttpPut]
        public async Task<Result> Edit([FromBody]SysMenu sysMenu)
        {
            await _menuService.Update(sysMenu);
            return Result.Success();
        }

        #endregion

        #region 删除菜单
        [HasPermission("system:menu:remove")]
        [LogInfo(Title = "菜单管理",BusinessType = BusinessType.Delete)]
        [HttpDelete("{menuId}")]
        public async Task<Result> Delete([FromRoute]long menuId)
        {
            await _menuService.Delete(menuId);
            return Result.Success();
        }
        #endregion
    }
}